const app = getApp();
const utils = require('../../utils/utils.js')
let PAGE = {page: 1, page_size: 10};
Page({

    /**
     * 页面的初始数据
     */
    data: {
        // 订单总数量
        total: 0,
        // 存储商户信息
        nearyShops: []
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        PAGE = {page: 1, page_size: 10};
        // 获取附近商家信息
        this.getNearyShopInfo()
    },
    //
    getNearyShopInfo() {
        let that = this;
        wx.showLoading({
            content: 'loading...',
            mask: true
        });
        let newList = [];
        let oldList = that.data.nearyShops;
        utils.httpRequest('/getShopList', {
            longitude: app.globalData.longitude,
            latitude: app.globalData.latitude,
            page: PAGE.page,
            page_size: PAGE.page_size,
            isList: 1
        }, function (res) {
            if (res.code == 1) {
                that.setData({
                    total: res.data.total
                })
                if (PAGE.page > 1) {//合并原来的数据
                    newList = res.data.list;//新请求的数据
                    that.setData({
                        nearyShops: oldList.concat(newList)
                    })
                } else {
                    // 初始化数据
                    that.setData({
                        nearyShops: res.data.list
                    })
                }
            }
            wx.hideLoading();
        }, function () {
            wx.hideLoading();
        })
    },
    // =============  跳转商家界面 =========================
    goToShop(e) {
        let id = e.currentTarget.dataset.id;
        wx.navigateTo({
            url: '../shop/shop?id=' + id
        })
    },
    // ============== 获取下一页数据 =======================
    onReachBottom() {
        let that = this;
        if (that.data.total > that.data.nearyShops.length) {
            PAGE.page++;
            that.getNearyShopInfo();
        } else {
            return
        }
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})